package com.zhongkai.service.impl;

import com.mysql.jdbc.StringUtils;
import com.zhongkai.Bean.PageBean;
import com.zhongkai.app.service.IBaseDao;
import com.zhongkai.app.service.PageQuerDao;
import com.zhongkai.app.service.SetmodelService;
import com.zhongkai.model.utils.TTHelp;
import com.zhongkai.model.utils.ZhongkaiResult;
import com.zhongkai.model3.SetProduct;
import com.zhongkai.model3.Warehouse3;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公司产品
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SetmodelServiceImpl implements SetmodelService {


    @Autowired
    private IBaseDao iBaseDao;

    @Autowired
    private PageQuerDao pageQuerDao;

    /**
     * 产品添加
     *
     * @param name 产品名称
     * @param unit 单位
     * @param cost 成本价格
     */
    @Override
    public ZhongkaiResult addModel(String name, String unit, String cost) {
        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(unit) || StringUtils.isNullOrEmpty(cost)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        SetProduct setProduct = (SetProduct) iBaseDao.get("from SetProduct st where st.proName='" + name + "'");

        if (setProduct != null) {
            return ZhongkaiResult.build(405, "该产品已存在");
        }
        String uuid = TTHelp.generate();
        //公司产品库
        Warehouse3 warehouse = (Warehouse3) iBaseDao.get("from Warehouse3 wa where wa.warName='" + name + "'");

        if (warehouse == null) {
            warehouse = new Warehouse3();
            warehouse.setWarUuid(uuid);
            warehouse.setWarCount(0);
            warehouse.setWarName(name);
            iBaseDao.save(warehouse);

        }else {
            warehouse.setWarUuid(uuid);
            iBaseDao.update(warehouse);
        }
        try {
            setProduct = new SetProduct();
            setProduct.setSetUuid(uuid);
            setProduct.setProName(name);
            setProduct.setProUnit(unit);
            setProduct.setProCost(cost);
            setProduct.setProRemove("1");
            iBaseDao.save(setProduct);

            return ZhongkaiResult.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(403, "添加失败");
        }


    }

    /**
     * 产品修改
     *
     * @param name 产品名称
     * @param unit 单位
     * @param cost 成本价格
     * @param uuid 产品uuid
     */

    @Override
    public ZhongkaiResult updateModel(String name, String unit, String cost, String uuid) {
        if (StringUtils.isNullOrEmpty(name) || StringUtils.isNullOrEmpty(unit) || StringUtils.isNullOrEmpty(cost)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        SetProduct setProduct = (SetProduct) iBaseDao.get("from SetProduct st where st.setUuid='" + uuid + "'");

        Warehouse3 warehouse = (Warehouse3) iBaseDao.get("from Warehouse3 wa where wa.warUuid='" + uuid + "'");

        if (setProduct == null) {
            return ZhongkaiResult.build(405, "该产品不存在");
        }

        if (warehouse != null) {
            warehouse.setWarName(name);
            iBaseDao.update(warehouse);

        }

        setProduct.setProName(name);
        setProduct.setProUnit(unit);
        setProduct.setProCost(cost);

        iBaseDao.update(setProduct);

        return ZhongkaiResult.ok("修改成功");


    }


    /**
     * 产品删除
     *
     * @param uuid 产品id
     */
    @Override
    public ZhongkaiResult removeModel(String uuid) {
        if (StringUtils.isNullOrEmpty(uuid)) {
            return ZhongkaiResult.build(400, "数据不完整");
        }
        SetProduct setProduct = (SetProduct) iBaseDao.get("from SetProduct st where st.setUuid='" + uuid + "'");

        Warehouse3 warehouse = (Warehouse3) iBaseDao.get("from Warehouse3 wa where wa.warUuid='" + uuid + "'");

        if (setProduct == null) {
            return ZhongkaiResult.build(405, "该产品不存在");
        }


        try {

            iBaseDao.delete(setProduct);
            if (warehouse != null){
                iBaseDao.delete(warehouse);

            }
            //   iBaseDao.delete(setProduct);
            return ZhongkaiResult.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "删除失败");
        }


    }


    /**
     * 产品列表
     *
     * @param page 页数
     * @param rows 每页数据数量
     */
    @Override
    public ZhongkaiResult getModelList(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }


        try {
            PageBean pageBean = new PageBean();
            pageBean.setCurrentPage(page);
            pageBean.setPageSize(rows);
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SetProduct.class);
            detachedCriteria.add(Restrictions.eq("proRemove", "1")); //增加条件

            pageBean.setDetachedCriteria(detachedCriteria);
            pageQuerDao.pageQuery(pageBean);
            return ZhongkaiResult.ok(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return ZhongkaiResult.build(405, "查询失败");
        }

    }

/*    @Override
    public ZhongkaiResult getModelList2(Integer page, Integer rows) {
        if (page == 0 || rows == 0) {
            return ZhongkaiResult.build(400, "数据不完整");
        }

        List<SetProduct> list = iBaseDao.find("from SetProduct", page, rows);

        return ZhongkaiResult.ok(list);
    }*/


}
